﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class LPH : System.Web.UI.Page
    {
        private DataAccess<Object> dataAccess;
        private ReportDocument lph;

        public LPH()
        {
            dataAccess = new DataAccess<object>();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var query = Server.UrlDecode(Request.QueryString[0].ToString());
            var TglStart = Server.UrlDecode(Request.QueryString[1].ToString());
            var TglEnd = Server.UrlDecode(Request.QueryString[2].ToString());
            var Tipe = Server.UrlDecode(Request.QueryString[3].ToString());

            var Source =  dataAccess.GetDataListDataTable(query);
            lph = new ReportDocument();
            string reportPath = string.Empty;

            reportPath = Server.MapPath("~/Report/LapLPH.rpt");

            lph.Load(reportPath);
            lph.SetDataSource(Source);
            SetupParameter(TglStart,TglEnd);
            var param = new ParameterDiscreteValue();
            param.Value = Tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            lph.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = lph;
        }


        private void SetupParameter(string TglStart,string TglEnd)
        {
            var TitleSebelum = "MONTH TO DATE BULAN LALU";
            var TitleParField = lph.DataDefinition.ParameterFields["TitleSebelum"];
            var TitleParValues = TitleParField.CurrentValues;
            var TitleDisValue = new ParameterDiscreteValue();

        
            TitleDisValue.Value = TitleSebelum;
            TitleParValues.Clear();
            TitleParValues.Add(TitleDisValue);

           
            TitleParField.ApplyCurrentValues(TitleParValues);
            var TitleSesudah = "MONTH TO DATE BULAN INI"; 


       
            var Title2ParField = lph.DataDefinition.ParameterFields["TitleSesudah"];
            var Title2ParValues = Title2ParField.CurrentValues;
            var Title2DisValue = new ParameterDiscreteValue();

          

            Title2DisValue.Value = TitleSesudah;

          
            Title2ParValues.Clear();
            Title2ParValues.Add(Title2DisValue);

            Title2ParField.ApplyCurrentValues(Title2ParValues);


            var Tgl1ParField = lph.DataDefinition.ParameterFields["TglStart"];
            var Tgl1ParValues = TitleParField.CurrentValues;
            var Tgl1DisValue = new ParameterDiscreteValue();

            var Tgl2ParField = lph.DataDefinition.ParameterFields["TglEnd"];
            var Tgl2ParValues = Title2ParField.CurrentValues;
            var Tgl2DisValue = new ParameterDiscreteValue();


            Tgl1DisValue.Value = TglStart;
            Tgl2DisValue.Value = TglEnd;

            Tgl1ParValues.Clear();
            Tgl2ParValues.Clear();
            Tgl1ParValues.Add(Tgl1DisValue);
            Tgl2ParValues.Add(Tgl2DisValue);

            Tgl1ParField.ApplyCurrentValues(Tgl1ParValues);
            Tgl2ParField.ApplyCurrentValues(Tgl2ParValues);
        }


        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            lph.Clone();
            lph.Dispose();

        }
      
    }
}